package com.arox.springinaction.post;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.springframework.jms.support.JmsUtils;
import org.springframework.stereotype.Component;

@Component("mailListener")
public class MailListener implements MessageListener {

    public void onMessage(Message message) {
	MapMessage mapMessage = (MapMessage) message;
	try {
	    Mail mail = new Mail();
	    mail.setMailId(mapMessage.getString("mailId"));
	    mail.setCountry(mapMessage.getString("country"));
	    mail.setWeight(mapMessage.getDouble("weight"));
	    displayMail(mail);
	} catch (JMSException e) {
	    throw JmsUtils.convertJmsAccessException(e);
	}
    }

    private void displayMail(Mail mail) {
	System.out.println("Mail #" + mail.getMailId() + " received");
    }

}
